
H5x.component("pager",[],function(opts){   
    this.container =  opts.container;
    this.opts = opts;
    this.use("render");
    this.use("emitter");
    this.init();
},{
    init:function(){ 
        this.opts.pager = this.opts.pager || {page:0,size:10,total:0};
        var pager = this.opts.pager;
        //console.log(pager);
        pager.pages = Math.ceil(pager.total/pager.size);
        this.render("tp-pager",{
            pager:pager
        });

        this.bindAction();
    },
    bindAction:function(){
        var pager = this.opts.pager;
        var opts = this.opts;
        this.container.find(".previous").on("click",function(){
            var page = parseInt(pager.page) - 1;
            if(page < 0){
                $.tip("已经是第一页！");
                return ;
            }

            if(typeof opts.onchange == "function"){
                opts.onchange(page,pager.page);
            }
        });

        this.container.find(".next").on("click",function(){
            var page = parseInt(pager.page) + 1;
            if(page >= pager.pages){
                $.tip("已经是最后一页！");
                return ;
            }

            if(typeof opts.onchange == "function"){
                opts.onchange(page,pager.page);
            }
        });

        this.container.find(".page_no").on("click",function(){
            if($(this).hasClass("active")) return ;
            var page = parseInt($(this).attr("data-page"));
            if(typeof opts.onchange == "function"){
                opts.onchange(page,pager.page);
            }
        })
    }
});